/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/4 22:26
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

(function () {

    "use strict";

    var api = {
        saveThumbUrl: "/farm/base/upThumb?type=base&extra=slider",
        adminDetail: "/admin/user/detail",
        rolesUrl: "/admin/role/getRoles",
        editUrl: "admin/user/editAdmin"
    };

    sm.controller('admin-user-edit', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.id = GetQueryString('id');

        $scope.detail = {
            id: $scope.id,
            thumb: "",
            nickname: "",
            email: "",
            password: "",
            phone: "",
            roleid: "",
            role: ""
        };

        $scope.roles = [];
        $scope.getRoles = function () {
            $http.get(api.rolesUrl).success(function (res) {
                if (res.state) {
                    $scope.roles = res.data;
                    $scope.detail.roleid = $scope.roles[0]['id'];
                    $scope.detail.role = $scope.roles[0]['name'];
                    doGetDetail($scope.id);
                }
            });
        };

        function doGetDetail(id) {
                $http.get(api.adminDetail, {
                    params: {id: id}
                }).success(function (res) {
                    if (res.state) {
                        var data = res.data;
                        $scope.detail.thumb = data.avatar;
                        $scope.detail.nickname = data.nickName;
                        $scope.detail.email = data.email;
                        $scope.detail.phone = data.phone;
                        $scope.thumbView = data.thumbView;

                        angular.forEach($scope.roles, function (val) {
                            if (val.id == data.roleId) {
                                $scope.detail.roleid = val.id;
                                $scope.detail.role = val.name;
                            }
                        })
                    }
                });
        }

        $scope.getRoles();

        $scope.selectRole = function (key) {
            var role = $scope.roles[key];
            $scope.detail.roleid = role['id'];
            $scope.detail.role = role['name'];
        };

        // =============================================
        var $ = jQuery, uploader;

        // 初始化Web Uploader
        uploader = WebUploader.create({
            auto: true,
            server: api.saveThumbUrl,
            pick: '#uploadImage',
            duplicate: true,
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,png'
            }
        });

        uploader.on( 'uploadSuccess', function(file, res) {
            if (res.state) {
                $scope.detail.thumb = res.data.key;
                var b = res.data.url;
                $scope.thumbView = b || '';
                $scope.$apply();
            } else {
                var error = res.error ? res.error : '上传失败';
                Modal.error(error);
            }
        });

        // ===========end==================================

        // 保存数据
        $scope.saveAdmin = function () {

            if (!$scope.detail) {
                return false;
            }

            // 验证数据
            if (!checkParams()) {
                Modal.alert($scope.error);
                return false;
            }

            var url = api.editUrl;
            $http.post(url, $scope.detail).success(function (res) {
                if (res.state) {
                    Modal.success('编辑成功');
                } else {
                    var error = res.error ? res.error : '编辑失败';
                    Modal.error(error);
                }
            });

        };

        function checkParams() {

            var detail = $scope.detail;

            if (!detail.nickname) {
                Modal.error('用户昵称不能为空');
                return false;
            }

            if (!detail.email) {
                Modal.error('用户邮箱不能为空');
                return false;
            }

            if (!detail.phone) {
                Modal.error('联系方式不能为空');
                return false;
            }

            return true;
        }


    }]);

})();